package com.krui.iweibo.ui;

import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.krui.iweibo.R;

public class LogoActivity extends BaseActivity {

	private ImageView icon_one, icon_two, icon_three;
	private static final int LOGO_TIME = 3000;
	private static int icon_index = 0;
	private boolean update = true,login_state = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 去掉标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 全屏显示
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.logo);

		icon_one = (ImageView) findViewById(R.id.loding_icon_one);
		icon_two = (ImageView) findViewById(R.id.loding_icon_two);
		icon_three = (ImageView) findViewById(R.id.loding_icon_three);

		login_server();

	}

	/**
	 * 调用登陆界面
	 */
	private void login_server(){
		
		final long enter_time = System.currentTimeMillis();
		new Thread(new Runnable() {

			@Override
			public void run() {
				while (update) {
					try {
						Thread.sleep(300);
						
						icon_index = (++icon_index) % 3;
						//更新ICON
						updateIcon();

						if (System.currentTimeMillis() - enter_time > LOGO_TIME && !login_state) {
							
							intent2Where(LogoActivity.this, LoginActivity.class,true);
							login_state = true;
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}

		}).start();
	}
	
	
	/**
	 * 更新加载图标
	 */
	private void updateIcon() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				if (icon_index == 0) {
					icon_one.setBackgroundResource(R.drawable.loading_icon_activate);
					icon_two.setBackgroundResource(R.drawable.loading_icon_default);
					icon_three
							.setBackgroundResource(R.drawable.loading_icon_default);
				} else if (icon_index == 1) {
					icon_one.setBackgroundResource(R.drawable.loading_icon_default);
					icon_two.setBackgroundResource(R.drawable.loading_icon_activate);
					icon_three
							.setBackgroundResource(R.drawable.loading_icon_default);
				} else if (icon_index == 2) {
					icon_one.setBackgroundResource(R.drawable.loading_icon_default);
					icon_two.setBackgroundResource(R.drawable.loading_icon_default);
					icon_three
							.setBackgroundResource(R.drawable.loading_icon_activate);
				}
			}

		});

	}
}